import 'package:auto_route/annotations.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:sonora/config/sj_app_initial.dart';
import 'package:sonora/events/sj_video_event.dart';
import 'package:sonora/models/sj_config_data.dart';
import 'package:sonora/services/sj_api_config_service.dart';
import 'package:sonora/shared/lib/constants/sj_colors.dart';
import 'package:sonora/utils/sj_log_utils.dart';
import 'package:sonora/utils/sj_storage_utils.dart';
import 'package:sonora/views/appbar/sj_appbar.dart';
import 'package:sonora/views/sj_home_page.dart';
import 'package:sonora/views/sj_medal_page.dart';
import 'package:sonora/views/sj_mine_page.dart';
import 'package:sonora/views/sj_root_provider.dart';
import 'package:sonora/views/sj_video_page.dart';

@RoutePage()
class SjMainScreen extends StatefulWidget {
  static final GlobalKey<SjMainScreenState> globalKey = GlobalKey<SjMainScreenState>();

  const SjMainScreen({super.key});

  @override
  State<StatefulWidget> createState() => SjMainScreenState();
}

class SjMainScreenState extends State<SjMainScreen> {
  late PageController _pageController;
  static final List<Widget> _widgetOptions = <Widget>[
    const SjHomePage(),
    const SjVideoPage(),
    SjMedalPage(),
    const SjMinePage()
  ];

  void _onItemTapped(int index) {
    setState(() {
    });
  }

  void changeTab(int index) {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _onItemTapped(index);
    });
  }

  @override
  void initState() {
    // 还原系统 UI 模式
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
    _pageController = PageController();

    // Listen to changes in the PageProvider to update the PageController
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final pageProvider = Provider.of<SjRootProvider>(context, listen: false);
      _pageController.jumpToPage(pageProvider.index);
    });
    super.initState();
  }

   @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // Listen to PageProvider changes and update PageController
    Provider.of<SjRootProvider>(context).addListener(() {
      final pageProvider = Provider.of<SjRootProvider>(context, listen: false);
      if (pageProvider.index != _pageController.page?.round()) {
        _pageController.jumpToPage(pageProvider.index);
      }
    });
  }

  void _onPageChanged(int index) {
    final pageProvider = Provider.of<SjRootProvider>(context, listen: false);
    pageProvider.changeIndex(index);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: PageView(
          physics: const NeverScrollableScrollPhysics(),
          controller: _pageController,
          onPageChanged: _onPageChanged,
          children: _widgetOptions,
        ),
        bottomNavigationBar: BottomNavigationBar(
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
                icon: Image.asset(
                  'assets/images/tab_home_normal.png',
                  width: 24,
                  height: 24,
                ),
                activeIcon: Image.asset(
                  'assets/images/tab_home_selected.png',
                  width: 24,
                  height: 24,
                ),
                label: ''),
            BottomNavigationBarItem(
                icon: Image.asset(
                  'assets/images/tab_video_normal.png',
                  width: 24,
                  height: 24,
                ),
                activeIcon: Image.asset(
                  'assets/images/tab_video_selected.png',
                  width: 24,
                  height: 24,
                ),
                label: ''),
            BottomNavigationBarItem(
                icon: Image.asset(
                  'assets/images/tab_medal_normal.png',
                  width: 24,
                  height: 24,
                ),
                activeIcon: Image.asset(
                  'assets/images/tab_medal_selected.png',
                  width: 24,
                  height: 24,
                ),
                label: ''),
            BottomNavigationBarItem(
                icon: Image.asset(
                  'assets/images/tab_mine_normal.png',
                  width: 24,
                  height: 24,
                ),
                activeIcon: Image.asset(
                  'assets/images/tab_mine_selected.png',
                  width: 24,
                  height: 24,
                ),
                label: ''),
          ],
          currentIndex: Provider.of<SjRootProvider>(context).index,
          onTap: (index) {
            if (index != 1) {
              eventBus.fire(SjVideoBusEvent(isPlay: false));
            }
            _pageController.jumpToPage(index);
            if (index == 0) {
              SjLogPvManger.stat(page: SjLogPages.sjPageNameHomeTab);
            } else if (index == 1) {
              SjLogPvManger.stat(page: SjLogPages.sjPageNameVideoTab);
            } else if (index == 2) {
              SjLogPvManger.stat(page: SjLogPages.sjPageNameMedalTab);
            } else if (index == 3) {
              SjLogPvManger.stat(page: SjLogPages.sjPageNameMineTab);
            }

          },
          type: BottomNavigationBarType.fixed,
          backgroundColor: SjColors.blackColor,
        ));
  }
}
